/**
 * Created by zhangxiaoyu on 16/12/07.
 */
angular.module('hyt.myOrderService', ['ngResource'])

  .factory('Orders', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getOrders,
      orderData={},pageSize=10;

    var resource = $resource(ApiUrl+':orderSearch',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getOrderData : function(orderSearch){
        var hasNextPage = true;
        resource.query({
          orderSearch:JSON.stringify({
            pageSize:'10',
            pageNo:'1',
            parameter:orderSearch.parameter,
            userMsg:orderSearch.userMsg
          })
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            if(r.rows.length < pageSize ){
              hasNextPage = false;
            }
            orderData[pageSize] = {
              hasNextPage : hasNextPage,
              nextPage : 2,
              data : r.rows
            };
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('OrderList.Updated');
        })
      },
      getResult:function(){
        if(orderData[pageSize]===undefined){
          return false;
        }
        return orderData[pageSize].data;
      },
      getMoreOrderData : function(orderSearch){
        if(orderData[pageSize] === undefined){
          return false;
        }
        var hasNextPage = orderData[pageSize].hasNextPage;
        var nextPage = orderData[pageSize].nextPage;
        var moreOrderData = orderData[pageSize].data;
        resource.query({
          orderSearch:JSON.stringify({
            pageSize:'10',
            pageNo:nextPage.toString(),
            parameter:orderSearch.parameter,
            userMsg:orderSearch.userMsg
          })
        },function(r){
          console.log(r);
          nextPage++;
          if(r.rows.length < pageSize ){
            hasNextPage = false;
          }
          moreOrderData = moreOrderData.concat(r.rows);

          orderData[pageSize] = {
            hasNextPage : hasNextPage,
            nextPage : nextPage,
            data : moreOrderData
          };
          $rootScope.$broadcast('OrderList.Updated');
        })
      },
      hasNextPage: function() {
        if (orderData[pageSize] === undefined) {
          return false;
        }
        return orderData[pageSize].hasNextPage;
      }
    }
  })
  .factory('RevokeOrder', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.cancelOrder,
      result;

    var resource = $resource(ApiUrl+':revokeOrder',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getRevokeOrder : function(revokeOrder){
        return resource.query({
          revokeOrder:revokeOrder
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('RevokeOrder.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('SubmitOrder', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.submitOrder,
      result;

    var resource = $resource(ApiUrl+':submitOrder',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getSubmitOrder : function(submitOrder){
        return resource.query({
          submitOrder:submitOrder
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('SubmitOrder.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('OrderDetail', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getOrderDetail,
      result;

    var resource = $resource(ApiUrl+':orderDetail',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getOrderDetail : function(orderDetail){
        return resource.query({
          orderDetail:orderDetail
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('OrderDetail.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
  .factory('OrderRecord', function($resource, $rootScope,ENV,$ionicLoading,$state) {
    var ApiUrl = ENV.postUrl+ENV.api.getOrderRecord,
      result;

    var resource = $resource(ApiUrl+':orderRecord',{},{
      query :{
        method : 'get',
        isArray : false
      }
    });
    return{
      getOrderRecord : function(orderRecord){
        return resource.query({
          orderRecord:orderRecord
        },function(r){
          console.log(r);
          if (r.msg!="0003"){
            result=r;
          }else {
            $ionicLoading.show({
              noBackdrop: true,
              template: "您的账号已在其他设备登录",
              duration: 1500
            });
            $state.go('login');
          }
          $rootScope.$broadcast('OrderRecord.Updated');
        })
      },
      getResult:function(){
        return result;
      }
    }
  })
